package com.itheima.dao.impl;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.utils.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public class UserDaoImpl implements UserDao {

    @Override
    public List<User> findAll() {
        List<User> list= new ArrayList<>();

        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            connection = JDBCUtils.getConnection();
            String sql = "select * from user";
            statement = connection.prepareStatement(sql);
            rs = statement.executeQuery();

            while(rs.next()){
                list.add(new User(rs.getInt("id"), rs.getString("name"),
                        rs.getString("gender"),rs.getInt("age"),
                        rs.getString("address"),rs.getString("qq"),
                        rs.getString("email"),rs.getString("username"),
                        rs.getString("password")));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(connection,statement,rs);
        }



        return list;
    }

    @Override
    public void save(User user) {
        Connection connection = null;
        PreparedStatement st = null;

        try {
            connection = JDBCUtils.getConnection();
            String sql = "insert into user values(null,?,?,?,?,?,?,null,null)";
            st = connection.prepareStatement(sql);
            // 给? 占位符赋值
            st.setString(1,user.getName());
            st.setString(2,user.getGender());
            st.setInt(3,user.getAge());
            st.setString(4,user.getAddress());
            st.setString(5,user.getQq());
            st.setString(6,user.getEmail());

            st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
           JDBCUtils.close(connection,st,null);
        }


    }

    @Override
    public void deleteById(String id) {
        Connection connection = null;
        PreparedStatement statement = null;

        try {
            connection = JDBCUtils.getConnection();
            String sql = "delete from user where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,Integer.parseInt(id));

            statement.executeLargeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(connection,statement,null);
        }

    }

    @Override
    public User findById(String id) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        User user = null;

        try {
            connection = JDBCUtils.getConnection();
            String sql = "select * from user where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,Integer.parseInt(id));

            rs = statement.executeQuery();

            if(rs.next()){
                user = new User(rs.getInt("id"), rs.getString("name"),
                        rs.getString("gender"), rs.getInt("age"),
                        rs.getString("address"), rs.getString("qq"),
                        rs.getString("email"), rs.getString("username"),
                        rs.getString("password"));
            }
            System.out.println(user);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
           JDBCUtils.close(connection,statement,rs);
    }
      return user;
}

    @Override
    public void update(User user) {
        Connection connection = null;
        PreparedStatement statement = null;

        try {
            connection = JDBCUtils.getConnection();

            String sql = "update user set name = ?,gender = ? ,age = ? , address = ? , qq = ?, email = ? where id = ?";

            statement = connection.prepareStatement(sql);
            statement.setString(1, user.getName());
            statement.setString(2, user.getGender());
            statement.setInt(3, user.getAge());
            statement.setString(4, user.getAddress());
            statement.setString(5, user.getQq());
            statement.setString(6, user.getEmail());
            statement.setInt(7, user.getId());

            statement.executeLargeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(connection,statement,null);
        }

    }
}
